package com.deeper.customview.ui.c1.exercise;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.deeper.customview.BaseFragment;
import com.deeper.customview.R;

/**
 * 请在 ImageView 上绘制尺寸为 20	*	20 的实心圆， 圆的填充
 * 颜色随机， 从左上角开始， 从左往右从上往下直到画满整个屏幕为止
 */
public class Chapter01Exercise01Fragment extends BaseFragment {

    private ImageView iv;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_chapter01_exercise01, container, false);

        iv = root.findViewById(R.id.iv);
        int bitmapWidth = 500;
        int bitmapHeight = 800;
        int radius = 10;
        Bitmap bmp = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        for (int i = 0; i < Math.floor(bitmapWidth / (2.0 * radius)); i++) {
            for (int j = 0; j < Math.floor(bitmapHeight / (2.0 * radius)); j++) {
                float centerX = radius * (2 * i + 1);
                float centerY = radius * (2 * j + 1);
                paint.setColor(Color.rgb((float) (Math.random() * 255), (float) (Math.random() * 255), (float) (Math.random() * 255)));
                canvas.drawCircle(centerX, centerY, radius, paint);
            }
        }

        iv.setImageBitmap(bmp);
        return root;
    }
}
